#include <iostream>
#include <vector>
#include <string>

using namespace std;

int magicalString(int n) {
    string ret = "122"; int i = 2, signal = -1, num[3] = {1, 1, 2};    // 初始指针放置在第三位，数组存放为1，count和2，下一个出现数字为1
    while(ret.size() < n){
        for(int j = 0; j < (ret[i]-'0'); j++){
            ret += (num[1+signal] + '0');
        }
        if(signal == -1) num[1] += ret[i]-'0';
        signal *= -1; i++;
    }
    if(ret.size() > n && signal == 1) num[1]--; 
    return num[1];
}

int main(){
    int n = 7;
    cout << magicalString(n) << endl;
    return 0;
}
